import { getSimcardList, getSimExportUrl } from '../services/api';

export default {
  namespace: 'simcard',

  state: {
    list: [],
    filteredList: [], // used by modal list display
    onDevNum: 0,
    total: 0,
    pagination: {
      current: 1,
      pageSize: 10,
    },
    filteredPagination: {
      current: 1,
      pageSize: 10,
    },
    isLoading: false,
    hasMore: false,
  },
  effects: {
    // *fetchSimCardList({ payload = {} }, { call, put }) {
    //   const {
    //     current = 1,
    //     pageSize = 10,
    //     iccid = '',
    //     devId = '',
    //     startTime = '',
    //     endTime = '',
    //     online = '',
    //     status = '',
    //   } = payload;
    //   yield put({
    //     type: 'setLoading',
    //     payload: true,
    //   });
    //   if (payload) {
    //     yield put({
    //       type: 'setPagination',
    //       payload: current,
    //     });
    //   }
    //   const params = {
    //     page: current,
    //     pageSize,
    //     iccid,
    //     devId,
    //     startTime,
    //     endTime,
    //   };
    //   if (online !== '') {
    //     params.online = online === 'true' ? 1 : 0;
    //   }
    //   if (status !== '') {
    //     params.status = status === 'ACTIVATED' ? 1 : 0;
    //   }
    //   const rsp = yield call(getSimcardList, params);

    //   yield put({
    //     type: 'setList',
    //     payload: rsp.data,
    //   });

    //   yield put({
    //     type: 'setLoading',
    //     payload: false,
    //   });
    // },
    *fetchSimCardList({ payload }, { call, put }) {
      // const {
      //   online,
      //   status
      // } = payload;
      // yield put({
      //   type: 'setLoading',
      //   payload: true,
      // });
      // if (payload) {
      //   yield put({
      //     type: 'setPagination',
      //     payload: current,
      //   });
      // }
      // const params = {
      //   page: current,
      //   pageSize,
      //   iccid,
      //   devId,
      //   startTime,
      //   endTime,
      // };
      // if (online !== '') {
      //   params.online = online === 'true' ? 1 : 0;
      // }
      // if (status !== '') {
      //   params.status = status === 'ACTIVATED' ? 1 : 0;
      // }
      const rsp = yield call(getSimcardList, payload);

      yield put({
        type: 'setList',
        payload: rsp.data,
      });

      yield put({
        type: 'setLoading',
        payload: false,
      });
    },
    *onExport({ payload, callback }, { call, put }) {
      const rsp = yield call(getSimExportUrl, payload);
      console.log(rsp);
      if (callback) callback(rsp);
    }
  },
  reducers: {
    setList(state, action) {
      return {
        ...state,
        list: action.payload.list,
        total: action.payload.total,
      };
    },
    setFilteredList(state, action) {
      return {
        ...state,
        filteredList: [...state.filteredList, ...action.payload.list],
        hasMore: action.payload.hasMore,
      };
    },
    setPagination(state, action) {
      return {
        ...state,
        pagination: {
          ...state.pagination,
          current: action.payload,
        },
      };
    },
    setLoading(state, action) {
      return {
        ...state,
        isLoading: action.payload,
      };
    },
    setFilteredPagination(state, action) {
      return {
        ...state,
        filteredPagination: {
          ...state.filteredPagination,
          current: action.payload,
        },
      };
    },
    resetFilteredList(state) {
      return {
        ...state,
        filteredList: [],
        filteredPagination: {
          current: 1,
          pageSize: 10,
        },
      };
    },
  },
};
